package com.aistar.service.Impl;

import com.aistar.mapper.ResumeMapper;
import com.aistar.pojo.*;
import com.aistar.service.*;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ResumeServiceImpl implements ResumeService {
    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private WorkExperienceService workExperienceService;

    @Autowired
    private ProjectExperienceService projectExperienceService;

    @Autowired
    private HonorService honorService;

    @Autowired
    private SkillService skillService;

    @Override
    public ServerResponse add(Resume resume) {

        ServerResponse response = getCountByCustId(resume.getCustId(),resume.getResumeName());
        if (response.getStatus()==1){// 简历不存在
            int rows = resumeMapper.insert(resume);

            if (rows>0)
                return ServerResponse.addSuccess();
            return ServerResponse.addFailed();
        }else{
            return response; //return ServerResponse.getDataFailed("该简历名称已存在，请重命名简历名称");
        }
    }

    @Override
    public ServerResponse modify(Resume resume) {

        //update set resume_name =? , resume_update_time = ? where resume_id= ?
        int rows =  resumeMapper.updateByPrimaryKeySelective(resume);

        if (rows>0)
            return ServerResponse.updateSuccess();
        return ServerResponse.updateFailed();
    }

    @Override
    public ServerResponse modifyStatus(Resume resume) {
        return null;
    }

    @Override
    public ServerResponse getById(Integer id) {


        Resume resume = resumeMapper.selectByPrimaryKey(id);

        return ServerResponse.getDateSuccess(resume);
    }

    @Override
    public ServerResponse getAll() {
        return null;
    }

    @Override
    public ServerResponse getCountByCustId(Integer custId) {

        //   select count(resume_id) from resume where cust_id = ? and status = 0

        ResumeExample example = new ResumeExample();
        example.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0);

        int count = resumeMapper.countByExample(example);

        System.out.println("该用户已有建立个个数：" + count);
        if (count < 3)
            return ServerResponse.getDateSuccess("可以创建简历");
        return ServerResponse.getDataFailed("最多只能创建3份简历");
    }

    @Override
    public ServerResponse getCountByCustId(Integer custId, String resumeName) {

        ResumeExample example = new ResumeExample();
        example.createCriteria().andCustIdEqualTo(custId).andResumeNameEqualTo(resumeName).andStatusEqualTo(0);
        int count = resumeMapper.countByExample(example);

        System.out.println("该用户的已有简历的个数：" + count);
        if (count ==0)
            return ServerResponse.getDateSuccess("简历不存在");
        return ServerResponse.getDataFailed("该简历名称已存在，请重命名简历名称");
    }
    //查询当前用户最新添加的简历
    @Override
    public ServerResponse getLatestOne(Integer CustId) {

        ResumeExample resumeExample = new ResumeExample();
        resumeExample.createCriteria().andCustIdEqualTo(CustId).andStatusEqualTo(0) ;
        resumeExample.setOrderByClause("resume_create_time desc");
        //排序了所以查询了很多，需要get(0)
        Resume resume = resumeMapper.selectByExample(resumeExample).get(0);
        return ServerResponse.getDateSuccess(resume);
    }

    @Override
    public ServerResponse getAllByCustId(Integer custId) {

        ResumeExample example =  new ResumeExample();
        example.createCriteria().andCustIdEqualTo(custId).andStatusEqualTo(0);

        List<Resume> resumeList = resumeMapper.selectByExample(example);

        for (Resume resume:resumeList){
            System.out.println(resume);
        }
        return ServerResponse.getDateSuccess(resumeList);
    }

    @Override
    public ServerResponse getResumeAllVOById(Integer resumeId) {
        ResumeAllVO resumeAllVO = new ResumeAllVO();

        //添加Resume
        Resume resume = resumeMapper.selectByPrimaryKey(resumeId);
        resumeAllVO.setResume(resume);

        //添加workExperience
        //根据简历id 获得 所有 workExperience   //	select * from work_experience where resumeId = 85 and status  =0

        ServerResponse workExperienceServerResponse = workExperienceService.getAllByResumeId(resumeId);
        if (workExperienceServerResponse.getStatus() == 1) {
            List<WorkExperience> workExperienceList = (List<WorkExperience>) workExperienceServerResponse.getData();
            resumeAllVO.setWorkExperienceList(workExperienceList);
        }
        //根据简历id 获得 所有 projectExperience

        ServerResponse projectExperienceServerResponse = projectExperienceService.getAllByResumeId(resumeId);
        if (projectExperienceServerResponse.getStatus() == 1) {
            List<ProjectExperience> projectExperienceList = (List<ProjectExperience>) projectExperienceServerResponse.getData();
            resumeAllVO.setProjectExperienceList(projectExperienceList);
        }

        //根据简历id 获得 所有 honor

        ServerResponse hornorServerResponses = honorService.getAllByResumeId(resumeId);
        if (hornorServerResponses.getStatus() == 1) {
            List<Honor> honorList = (List<Honor>) hornorServerResponses.getData();
            resumeAllVO.setHonorList(honorList);
        }
        //根据简历id 获得 所有 skill

        ServerResponse skillServerResponses = skillService.getAllByResumeId(resumeId);
        if (skillServerResponses.getStatus() == 1) {
            List<Skill> skillList = (List<Skill>) skillServerResponses.getData();
            resumeAllVO.setSkillList(skillList);
        }
        return ServerResponse.getDateSuccess(resumeAllVO);
    }
}